package xyz.fuqufyb.aec.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.fuqufyb.aec.data.JsonResult;
import xyz.fuqufyb.aec.data.TableResult;
import xyz.fuqufyb.aec.entity.Customer;
import xyz.fuqufyb.aec.service.ICustomerService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 用户: fyb
 * @description class: CustomerController
 * @date 时间: 2021/2/1 22:10
 */

@RestController
@RequestMapping("/sys/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    /**
     *分页查询客户数据 模糊搜索名称和状态
     * @param maps
     * @return
     */
    @RequestMapping("/table")
    public TableResult table(@RequestParam Map<String, Object> maps) {
        try {
            TableResult tableResult = customerService.getPageCustomer(maps);
            return tableResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return TableResult.getTableResultError(301, "客户管理数据出错");
    }

    /**
     * 新增 客户 数据
     * @param customer
     * @return
     */
    @PostMapping("/add")
    public JsonResult addCustomer(Customer customer) {
        int i = customerService.insertSelective(customer);
        if (i > 0) {
            return JsonResult.getJsonSuccess("新增成功");
        }
        return JsonResult.getJsonError();

    }

    /**
     * 修改客户数据
     * @param customer
     * @return
     */
    @PostMapping("/update")
    public JsonResult updateCustomer(Customer customer) {
        int i = customerService.updateByPrimaryKeySelective(customer);
        if (i > 0) {
            return JsonResult.getJsonSuccess("更新成功");
        }
        return JsonResult.getJsonError();
    }

    /**
     * 批量删除客户数据
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public JsonResult delCustomer(@RequestParam("ids") Integer[] ids) {
        int i = customerService.deleteByCustomerIds(Arrays.asList(ids));
        if (i > 0) {
            return JsonResult.getJsonSuccess("删除成功");
        }
        return JsonResult.getJsonError();
    }

    /**
     * 全查 数据给其他界面需要关联的数据
     * @return
     */
    @PostMapping("/tree")
    public List<Customer> getTrees(){
         return  customerService.getListCustomer();
    }

}
